/**
 * Progetto PRAISED
 * 15/dic/2010
 */
package it.uniroma3.dia.praised.pubmed.crawler;

import it.uniroma3.dia.praised.exception.CrawlerException;
import it.uniroma3.dia.praised.exception.FactoryException;
import it.uniroma3.dia.praised.factory.WebClientFactory;
import it.uniroma3.dia.praised.pubmed.model.Article;
import it.uniroma3.dia.praised.pubmed.parser.ParserPubMed;
import it.uniroma3.dia.praised.pubmed.wrapper.WrapperPubMed;
import it.uniroma3.dia.praised.util.SerializeXML;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 *
 */
public class CrawlerPubMedWorker {

	private WrapperPubMed wrapperPubMed;
	
	public CrawlerPubMedWorker() {
		this.wrapperPubMed = new WrapperPubMed();
	}

	/**
	 * 
	 * @param url
	 * @param numResultIntoPage
	 * @return list
	 * @throws FactoryException
	 * @throws CrawlerException
	 */
	public List<Article> getArticleSnippet(URL url, int numResultIntoPage) throws FactoryException, CrawlerException {
		List<Article> list = new ArrayList<Article>();
		Article article;
		ParserPubMed parser = new ParserPubMed();
		
		HtmlPage page = WebClientFactory.newHtmlPage(url);
		
		for(int i=1; i<=numResultIntoPage; i++) {
			article = new Article();
			article.setPMCID(parser.retrievePMCID(parser.getPatternItem(page, "/html/body/form/table[2]/tbody/tr/td/table/tbody/tr[4]/td/div/div[2]/div[2]/table[" + i + "]/tbody/tr/td[2]/div/div/div[2]")));
			article.setTitle(parser.getPatternItem(page, "/html/body/form/table[2]/tbody/tr/td/table/tbody/tr[4]/td/div/div[2]/div[2]/table[" + i + "]/tbody/tr/td[2]/div/div/div/div"));
			article.setAuthors(parser.getPatternItem(page, "/html/body/form/table[2]/tbody/tr/td/table/tbody/tr[4]/td/div/div[2]/div[2]/table[" + i + "]/tbody/tr/td[2]/div/div/div/div[2]"));
			article.setUrlMap(parser.getArticleLinks(page, article.getPMCID()));
			
			list.add(article);
		}
		
		return list;
	}
	
	/**
	 * This method fill-out the entire article
	 * @param article
	 * @throws CrawlerException 
	 */
	public void completeArticle(Article article) throws CrawlerException {
		Map<String,URL> urlMap = article.getUrlMap();
		
		try {
						
			long start = System.currentTimeMillis(); //Istante iniziale di processamento 

			/* Check if the full-text is reachable for this article */
			if(urlMap.get("Full Text") != null) { // Se e' presente
				if(this.wrapperPubMed.wrapPageIntoArticle(article, urlMap.get("Full Text"))) { // Only whether the extraction result is <code>true</code>
					/* Save the article in XML file */
					SerializeXML save = new SerializeXML(article);
					save.serializeArticle();
				}
				else
					System.err.println("File " + article.getPMCID() + " ... not created!");
			}
			else {
				/* Se non ho il Full-Text allora wrappo il PDF */
				this.wrapperPubMed.wrapPdf(article, urlMap.get("pdf"));
			}
			
			long end = System.currentTimeMillis();
			
			// Time spent for processing the whole article 
			System.err.println("PROCESSED: " + article.getPMCID() + " (" + article.getSections().size() + ") " + " TIME: " + (end - start));
			

		} catch (CrawlerException e) {
			throw new CrawlerException(e.getMessage());
		}
		
	}
	
}
